package at.ac.tuwien.isis.infret.ue2;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;
import java.util.Vector;

import at.ac.tuwien.isis.infret.helper.DocumentContainer;
import at.ac.tuwien.isis.infret.ue2.interfaces.IHtmlOutput;

public class HtmlOutput implements IHtmlOutput {
	PrintWriter pw;
	public HtmlOutput(String filePath) throws IOException{
		 pw = new PrintWriter(new FileWriter(filePath));
	}
	@Override
	public void writeToHtml(
			HashMap<DocumentContainer, Double> finalResult,Vector<TreeMap<DocumentContainer,Double>> resultLists,
			Vector<String> queryList, String distanceFunction, Vector<String> featureFiles, int kBest) {
		pw.println("<html><body>");
		pw.println("Output File for Query: <br>");
		//System.out.println(queryList);
		for(String query : queryList)
			pw.print(query+" ");
		
		
		pw.println("<br><br>Using Distance Function: <br>");
		pw.println(distanceFunction);
		
		
			
		pw.println("<br><br>Using Index File(s): <br>");
		for( String feature : featureFiles)
			pw.println(feature+"<br>");
		pw.println("<br>Documents of minimal distance across all Indices<table Border>");
		
		DecimalFormat df = new DecimalFormat("#.####");
		
	//	for(TreeMap<Double, DocumentContainer> map : resultLists){

		pw.println("<tr>");
		pw.println("<td>Document Name</td>" +
				"<td>Minimum Distance</td>" +
				"<td>Sum of occurences <br>in kBest of each Index</td>" +
				"<td>Average Rank</td>" +
				"<td>Average Distance</td>");
		pw.println("</tr>");
		Iterator<Double> minIterator = finalResult.values().iterator();
		for(DocumentContainer doc : finalResult.keySet()){
			pw.println("<tr>");
			pw.println("<td>"+doc.getClassname()+"/"+doc.getDocumentname()+"</td>" +
					"<td>"+df.format(minIterator.next())+"</td>"+
					"<td>"+Statistics.SumofOcc(doc, resultLists,kBest)+"</td>" +
					"<td>"+df.format(Statistics.avgRank(doc, resultLists))+"</td>" +
					"<td>"+Statistics.avgDistance(doc, resultLists)+"</td>");
			pw.println("</tr>");
		}
		pw.println("</table>");
	//	}
		
		
		pw.println("</body></html>");
		pw.flush();
		pw.close();
	}


}
